<?php
require './library/db_constants.php';
require './library/planets.php';
require './library/terrain.php';
require './library/deposits.php';

// Call planets class for the createPlanet function.
$planets = new planets;
$terrain = new terrain;
$deposits = new deposits;

if(!isset($_GET['id'])) {
    echo 'Invalid id. <a href="listPlanets.php">Go back.</a>';
    exit();
}

// Store planet info in variable
$depositInfo = $deposits->depositInfo($_GET['id']);
$planetInfo = $planets->planetInfo($depositInfo['planet_id']);
$planetSize = $planetInfo['size'];


if(isset($_POST['save'])) {
    $deposits->editDeposit($_GET['id'], $_POST['deposit_name'], $_POST['deposit_size'], 
            $_POST['coordinates'], $_POST['raw_materials']);
}
?>
<!DOCTYPE html>
<html>
    <head>
        <title>SWC Prospecting Manager || Deposits</title>
        <link rel="stylesheet" href="./library/css/editDeposits.css" />
        <link rel="stylesheet" href="./library/css/createDeposit.css" />
    </head>
    <body>
        <h1>List/Edit Deposits</h1>
        
        <hr />
        
        <div id="planetInfo">
            <fieldset>
                <legend>Edit Planet || <?php echo $depositInfo['name'];?></legend>
                
                <form action='<?php echo $_SERVER['PHP_SELF'];?>?id=<?php echo $_GET['id'];?>' method='post'>
                    <label for='deposit_name'>Deposit Name: </label>
                    <input type='text' name='deposit_name' value='<?php echo $depositInfo['name'];?>'
                           size='30' maxlength='80'/>
                    <br />
                    <label for="deposit_size">Deposit Size: (no greater than 30) </label>
                    <input type="text" name="deposit_size" value='<?php echo $depositInfo['size'];?>'
                           size='30' maxlength='80'/>
                    <br />
                    <div id="planet">
                        <?php $deposits->drawDepositMap($depositInfo['planet_id'], $planetSize); ?>
                    </div>
                    <div id="editForm">
                        <label for="coordinates">Coordinates: </label>
                        <select name="coordinates">
                            <?php
                                for ($y = 0; $y <= $planetInfo['size']; $y++) {
                                    for ($x = 0; $x <= $planetInfo['size']; $x++) {
                                        if($depositInfo['coordinates'] == $x.", ".$y) {
                                            echo '<option selected value="'.$x.', '.$y.'">'.$x.', '.$y.'</option>';
                                        } else {
                                            echo '<option value="'.$x.', '.$y.'">'.$x.', '.$y.'</option>';
                                        }
                                    }
                                }
                            ?>
                        </select>
                        <br />
                        <br />
                        <label for="raw_materials">Raw Materials: </label>
                        <select name="raw_materials" id="raw_materials">
                            <option value="Quantum">Quantum</option>
                            <option value="Meleenium">Meleenium</option>
                            <option value="Ardanium">Ardanium</option>
                            <option value="Rudic">Rudic</option>
                            <option value="Ryll">Ryll</option>
                            <option value="Duracrete">Duracrete</option>
                            <option value="Alazhi">Alazhi</option>,
                            <option value="Laboi">Laboi</option>
                            <option value="Adegan">Adegan</option>
                            <option value="Rockivory">Rockivory</option>
                            <option value="Tibannagas">Tibannagas</option>
                            <option value="Nova">Nova</option>
                            <option value="Varium">Varium</option>
                            <option value="Varmigio">Varmigio</option>
                            <option value="Lommite">Lommite</option>
                            <option value="Hibridium">Hibridium</option>
                            <option value="Durelium">Durelium</option>
                            <option value="Lowickan">Lowickan</option>
                            <option value="Vertex">Vertex</option>
                            <option value="Berubian">Berubian</option>
                            <option value="Bacta">Bacta</option>
                        </select>
                        <br />
                        <br />
                        <input type="submit" name="save" value="Save Changes" />
                    </div>
                </form>
                
            </fieldset>
        </div>
        <footer>
            <a href="index.php">Home</a> || <a href="createPlanet.php">Create Planet</a> ||
            <a href="listPlanets.php">List/Edit Planets</a> || <a href="createDeposit.php">Create Deposit</a> ||
            <a href="listDeposits.php">List Deposits</a>
        </footer>
    </body>
</html>